#!/usr/bin/perl
#Programmer: Rory Carmichael
#Purpose: Plot rc-pipe data for a single gene using gnuplot
#NOTE: deprecated due to abandonment of p-value looper
#Usage: write_geneplot.pl <gene_record_file>
#Gene record files have the following columns in order:
#clustering probability	amino-acid	min-max-score	peak-distance

my $clusterfile = shift @ARGV;
chomp $clusterfile;
#print "$clusterfile\n";
my $width = `wc -l $clusterfile`;
chomp $width;
$width = $width - 2;
my $SX = 0.85*10;
my $SY = 0.25/($width/100)*10;
open(IF, "$clusterfile");
my $line = <IF>;
chomp $line;
my @splitline = split(/\t/, $line);
my $clustname = $splitline[1];
my $numseqs = ($#splitline-1)/3;
my $listend = $numseqs+2;
my @seqnames = @splitline[2..$listend];
print STDERR $line . "\n";
print STDERR "NUMSEQS: $numseqs\n";
my $NY=2+$numseqs;
$line = <IF>;
$line = <IF>;

my $DX = 0.4;
my $DY = 0.4;
my $size_x = $SX*1+$DX*1.5;
#$size_x .= "m";
my $size_y = $SY*$NY+$DY*1.8;
#$size_y .= "m";
my $px_sx = $size_x * 2500;
my $px_sy = $size_y * 2500;
#print "set terminal png size $px_sx,$px_sy font \"arial,28\"\n";
#print "set output '$clusterfile.visual.png'\n";
print "set terminal postscript eps enhanced size $size_x,$size_y color font \"arial,142\"\n";
#print "set terminal postscript enhanced color font \"arial,8\"\n";
#print "set output STDOUT\n";
print "set output '$clusterfile.visual.eps'\n";
#print "set terminal epslatex size $SX m, $SY m color colortext standalone 'phv,4' header '\\definecolor{t}{rgb}{0.5,0.5,0.5}'\n";
#print "set output '$clusterfile.tex'\n";
#print "set style line 11 lc rgb '#808080' lt 1\n";
#print "set border 3 front ls 11 lw 0.01\n";
#print "set style line 12 lc rgb '#808080' lt 0 lw 1\n";
#print "set grid back ls 12\n";
print "set nokey\n";

print "NX=1; NY=$NY\n";
print "MX=0.01; MY=0.01; SX=$SX; SY=$SY\n";
print "set bmargin MY; set tmargin MY; set lmargin MX; set rmargin MX\n";
print "DX=$DX; DY=$DY\n";
print "set border 3 lw 0.05\n";
print "set tics nomirror\n";
#print "set size $size_x,$size_y\n";
#print "set size SX*NX+DX*1.5,SY*NY+DY*1.8\n";
print "set size $size_x,$size_y\n";
print "set datafile missing \'-\'\n";
print "set multiplot\n";

my $xinc = int($width/10);
#Bottom figure (clustering confidence)
print "set size SX,SY\n";
print "set xrange[0:$width]\n";
print "set format y \"10^{\%L}\n";
print "set logscale y 10\n";
print "set yrange[0.0001:1.0]\n";
print "set ytics (0.001, 0.01, 0.1)\n";
print "set xtics $xinc\n";
print "set origin DX,DY\n";
#print "set grid\n";
print "plot '$clusterfile' using 2 lw 0.1 with filledcurves\n"; 

for(my $j=0;$j<$numseqs;$j++) {
	my $curname = $seqnames[$j];
	my $offset = $j+1;
	my $mm_i = 3+$j+$numseqs;
	my $peak_i = 2+$j+(2*$numseqs);
	#Next figure (min-max)
	print "set origin DX, DY+(SY*$offset)\n";
#	print "set border 3 lw 0.01\n";
	print "unset xtics\n";
	print "unset logscale y\n";
	print "set yrange[-100:100]\n";
	print "set ytics -80, 80\n";
	print "set label \"$curname\" at -35.0,DX*1.5+(SY*$offset) font \"arial,6\"\n";
#	print "set grid\n";
	print "unset format\n";
	print "set parametric\n";
	print "set trange [-100:100]\n";
	print "set style line 1 lw 3 lc rgb \"blue\"\n";
	print "set pointsize 4\n";
	print "plot [0:$width] t,0 with lines lw 0.1 lc rgb \"grey\"\n";
	print "plot '$clusterfile' using $mm_i ls 1 with points\n";
	my $i=1;
	foreach my $line (`cat $clusterfile`) {
		chomp $line;
		my $val = [split(/\t/, $line)]->[$peak_i];
		if ($val == 100) {
			print "plot $i,t lw 5 lc rgb \"red\"\n";
		}
		$i++;
	}
	print "unset label\n";
}
print "set nomultiplot\n";
